package com.micmac.customer.view.mediator
{
	
	import com.AppConst;
	import com.micmac.customer.events.CorpEvent;
	import com.micmac.customer.events.CustomerEvent;
	import com.micmac.customer.interfaces.ICustomerModule;
	import com.micmac.customer.interfaces.ICustomerService;
	import com.micmac.customer.model.CorpModel;
	import com.micmac.customer.service.CustomerService;
	import com.micmac.customer.view.CorporationView;
	import com.micmac.customer.vo.CorpVO;
	import com.micmac.customer.vo.OrgVO;
	import com.micmac.main.events.NotificationOnEvent;
	
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.FileReference;
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	import org.robotlegs.core.IMediator;
	import org.robotlegs.utilities.modular.mvcs.ModuleMediator;
	
	public class CorparationViewMediator extends ModuleMediator
	{
		[Inject]
		public var corparationView:CorporationView;
		private var _byteArray:ByteArray = new ByteArray;
		private var _fileRef:FileReference = new FileReference();
		[Inject]
		public var service:ICustomerService;
		[Inject]
		public var _dataModel:CorpModel;
		
		[Bindable]
		private var _corpIndex:int = 0;
		[Bindable]
		private var _currentStackIndex:int = 0;
		
		private static const TILE_INDEX:int = 2;
		private static const LIST_INDEX:int =1;
		
		override public function onRegister():void
		{
			addViewListener(CorporationView.BROWSE_IMG,onBrowse);
			addViewListener(CorporationView.CORP_SAVE,onCorpSave);
			addViewListener(CorporationView.CORP_PREVIOUS,onCorpPrevious);
			addViewListener(CorporationView.ADD_CORP,onAddCorp);
			addViewListener(CorporationView.CORP_NEXT,onCorpNext);
			addViewListener(CorporationView.SHOW_LIST_VIEW,onShowListView);
			addViewListener(CorporationView.SHOW_TILE_VIEW,onShowTileView);
			addViewListener(CorporationView.EDIT,onEdit)
			eventMap.mapListener(eventDispatcher,CustomerEvent.CORP_SAVED,onCorpSaved);
			
			eventMap.mapListener(corparationView,CorpEvent.CORP_EDIT_FROM_CORP_VIEW,onCorpEdit);
			eventMap.mapListener(eventDispatcher,CorpModel.CORP_EDITED,onCorpEdited);
			eventMap.mapListener(eventDispatcher,CorpEvent.CORP_EDIT_FROM_SERVICE,onEditSuccess);
			
			eventMap.mapListener(corparationView,CorpEvent.CORP_DELETE_FROM_CORP_VIEW,onDeleteCorp);
			eventMap.mapListener(eventDispatcher,CorpEvent.CORP_DELETE_FROM_SERVICE,onDeleteSuccess);
			eventMap.mapListener(eventDispatcher,CorpModel.CORP_DELETED,onCorpDeleted)
		}
		
		private function onBrowse(e:Event):void
		{
			_fileRef.browse();
			_fileRef.addEventListener(Event.SELECT, onFileSelect);
		}
		
		private function onFileSelect(event:Event):void
		{
			_fileRef.addEventListener(Event.COMPLETE, onLoadCmplete);
			_fileRef.load();	
		}
		
		private function onLoadCmplete(event:Event):void{
			_fileRef.removeEventListener(Event.SELECT, onFileSelect);
			_fileRef.removeEventListener(Event.COMPLETE, onLoadCmplete);
			_byteArray = new ByteArray();
			_byteArray = _fileRef.data;
			corparationView.imageView.browseImageSource = _byteArray;
			//var sendVars:URLVariables = new URLVariables();
			//sendVars.action = "upload";
			//sendVars.userID = _dataModel.domainerVO.user_id;
			//var request:URLRequest = new URLRequest();
			//	request.data = sendVars;
			//request.url = GlobalConsts.FILE_UPLOAD_URL;
			//request.method = URLRequestMethod.POST;
			
			//_fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,onCompleteData);
			//_fileRef.addEventListener(ProgressEvent.PROGRESS,onProgress);
			//_fileRef.addEventListener(IOErrorEvent.IO_ERROR,onError);
			//_fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityHandler);
			//_fileRef.upload(request,"file",false);
		}
		
		private function onCorpSave(event:Event):void
		{
			var corpVo:CorpVO = new CorpVO();
			corpVo.corpName = corparationView.corpNameText.text;
			corpVo.description = corparationView.descText.text;
			corpVo.addressLine1 = corparationView.addLine1Text.text;
			corpVo.addressLine2 = corparationView.addLine2Text.text;
			corpVo.addressLine3 = corparationView.addLine3Text.text;
			corpVo.zipCode = Number(corparationView.zipText.text);
			corpVo.phone = Number(corparationView.phoneText.text);
			corpVo.fax = Number(corparationView.faxText.text);
			corpVo.email = corparationView.emailText.text;
			corpVo.website = corparationView.webSiteText.text;
			corpVo.imgByte = this._byteArray;
			service.saveCorp(corpVo);
		}
		
			/*After success saving*/
		private function onCorpSaved(e:CustomerEvent):void
		{
			//corparationView.clearHandler();
			if(_dataModel.corpList.length != 1)
			{
				corparationView.prevBtn.enabled = true;
			}
			
			corparationView.addCorp.enabled = true;
			corparationView.saveBtn.enabled = false;
			_corpIndex = _dataModel.corpList.length - 1;
			corparationView.listViewBtn.enabled = true;
			corparationView.tileViewBtn.enabled = true;
			dispatchToModules(new NotificationOnEvent(NotificationOnEvent.NOTIFICATION_ON,"corp saved"));
		}
		
			/*To see saved data*/
		private function onCorpPrevious(e:Event):void
		{
			_corpIndex--;
			corparationView.nextBtn.enabled = true;
			corparationView.saveBtn.enabled = false;
			
			corparationView.corpNameText.text = _dataModel.corpList[_corpIndex].corpName;
			corparationView.descText.text = 	_dataModel.corpList[_corpIndex].description;
			corparationView.addLine1Text.text = _dataModel.corpList[_corpIndex].addressLine1;
			corparationView.addLine2Text.text = _dataModel.corpList[_corpIndex].addressLine2;
			corparationView.addLine3Text.text = _dataModel.corpList[_corpIndex].addressLine3;
			corparationView.zipText.text = _dataModel.corpList[_corpIndex].zipCode;
			corparationView.phoneText.text = _dataModel.corpList[_corpIndex].phone;
			corparationView.faxText.text = _dataModel.corpList[_corpIndex].fax;
			corparationView.emailText.text = _dataModel.corpList[_corpIndex].email;
			corparationView.webSiteText.text = _dataModel.corpList[_corpIndex].website;
			if(_dataModel.corpList[_corpIndex].imgByte.bytesAvailable == 0)
			{
				corparationView.defaultImageSet();
			}else
			{
				corparationView.imageView.browseImageSource = _dataModel.corpList[_corpIndex].imgByte;
			}
			if(_corpIndex == 0)
			{
				corparationView.prevBtn.enabled = false;
			}
		}
		
		private function onAddCorp(e:Event):void
		{
			corparationView.clearHandler();
			corparationView.saveBtn.enabled = true;
			corparationView.nextBtn.enabled = false;
			if(_dataModel.corpList.length != 1)
			{
				corparationView.prevBtn.enabled = true;
			}
			_corpIndex = _dataModel.corpList.length;
			_byteArray = new ByteArray();
		}
		
		private function onCorpNext(e:Event):void
		{
			_corpIndex++;
			corparationView.saveBtn.enabled = false;
			corparationView.prevBtn.enabled = true;
			corparationView.corpNameText.text = _dataModel.corpList[_corpIndex].corpName;
			corparationView.descText.text = 	_dataModel.corpList[_corpIndex].description;
			corparationView.addLine1Text.text = _dataModel.corpList[_corpIndex].addressLine1;
			corparationView.addLine2Text.text = _dataModel.corpList[_corpIndex].addressLine2;
			corparationView.addLine3Text.text = _dataModel.corpList[_corpIndex].addressLine3;
			corparationView.zipText.text = _dataModel.corpList[_corpIndex].zipCode;
			corparationView.phoneText.text = _dataModel.corpList[_corpIndex].phone;
			corparationView.faxText.text = _dataModel.corpList[_corpIndex].fax;
			corparationView.emailText.text = _dataModel.corpList[_corpIndex].email;
			corparationView.webSiteText.text = _dataModel.corpList[_corpIndex].website;
			if(_dataModel.corpList[_corpIndex].imgByte.bytesAvailable == 0)
			{
				//corparationView.imageView.imgMainContainer.visible = false;
				corparationView.defaultImageSet();
			}else
			{
				corparationView.imageView.browseImageSource = _dataModel.corpList[_corpIndex].imgByte;
			}
			if(_corpIndex == _dataModel.corpList.length - 1)
			{
				corparationView.nextBtn.enabled = false;
			}
		}
		
			/*The form view will change list*/
		private function onShowListView(e:Event):void
		{
			corparationView.corpVs.selectedIndex = LIST_INDEX;
			corparationView.currentState = 'listview';
			corparationView.CorpGrid.dataProvider = _dataModel.corpList;
		}
		
		private function onShowTileView(e:Event):void
		{
			corparationView.corpVs.selectedIndex = TILE_INDEX;
			corparationView.currentState = 'tileview';
			corparationView.tileVIew.tileDataProvider = new ArrayCollection();
			corparationView.tileVIew.tileDataProvider = _dataModel.corpList;
		}
		
		private function onCorpEdit(event:CorpEvent):void
		{
			
			var editCorpVO:CorpVO = event.editData as CorpVO;
			_currentStackIndex = corparationView.corpVs.selectedIndex;
			corparationView.corpVs.selectedIndex = 0;
			corparationView.currentState = 'edit';
			corparationView.corpNameText.text = editCorpVO.corpName;
			corparationView.descText.text = 	editCorpVO.description;
			corparationView.addLine1Text.text = editCorpVO.addressLine1;
			corparationView.addLine2Text.text = editCorpVO.addressLine2;
			corparationView.addLine3Text.text = editCorpVO.addressLine3;
			corparationView.zipText.text = String(editCorpVO.zipCode);
			corparationView.phoneText.text = String(editCorpVO.phone);
			corparationView.faxText.text = String(editCorpVO.fax);
			corparationView.emailText.text = editCorpVO.email;
			corparationView.webSiteText.text = editCorpVO.website;
			if(editCorpVO.imgByte.bytesAvailable == 0)
			{
				//corparationView.imageView.imgMainContainer.visible = false;
				corparationView.defaultImageSet();
			}else
			{
				corparationView.imageView.browseImageSource = editCorpVO.imgByte;
			}
		}
		
		private function onEdit(event:Event):void
		{
			var corpVo:CorpVO = new CorpVO();
			corpVo.corpName = corparationView.corpNameText.text;
			corpVo.description = corparationView.descText.text;
			corpVo.addressLine1 = corparationView.addLine1Text.text;
			corpVo.addressLine2 = corparationView.addLine2Text.text;
			corpVo.addressLine3 = corparationView.addLine3Text.text;
			corpVo.zipCode = Number(corparationView.zipText.text);
			corpVo.phone = Number(corparationView.phoneText.text);
			corpVo.fax = Number(corparationView.faxText.text);
			corpVo.email = corparationView.emailText.text;
			corpVo.website = corparationView.webSiteText.text;
			corpVo.imgByte = this._byteArray;
			service.editCorp(corpVo);
			//;
		}
		
		private function onEditSuccess(event:CorpEvent):void
		{
			var corpVo:CorpVO = event.editData as CorpVO;
			_dataModel.editingCorp = corpVo;
			/*var arr:ArrayCollection = corparationView.tileVIew.tileDataProvider;
			
			for (var i:int = 0;i<arr.length;i++)
			{
				if(arr[i].corpName == corpVo.corpName)
				{
					arr.removeItemAt(i);
					arr.refresh();
					arr.addItemAt(corpVo,i);
					arr.refresh();
				}
			}
			corparationView.corpVs.selectedIndex = _currentStackIndex;
			corparationView.tileVIew.tileDataProvider.refresh();
			if(_currentStackIndex == 1)
			{
				corparationView.currentState = 'listview';
			}else if(_currentStackIndex == 2)
			{
				corparationView.currentState = 'tileview';
			}*/
		}
		
		private function onCorpEdited(event:Event):void
		{
			corparationView.corpVs.selectedIndex = _currentStackIndex;
			
			_dataModel.corpList.refresh();
			if(_currentStackIndex == 1)
			{
				corparationView.currentState = 'listview';
			}else if(_currentStackIndex == 2)
			{
				corparationView.currentState = 'tileview';
			}
		}
		
		private function onDeleteCorp(event:CorpEvent):void
		{
			service.deleteCorp(event.editData);
		}
		
		private function onDeleteSuccess(event:CorpEvent):void
		{
			var corpVo:CorpVO = event.editData as CorpVO;
			_dataModel.deleteCorp = corpVo;
		}
		
		private function onCorpDeleted(event:Event):void
		{
			/*corparationView.corpVs.selectedIndex = _currentStackIndex;
			
			_dataModel.corpList.refresh();
			if(_currentStackIndex == 1)
			{
				corparationView.currentState = 'listview';
			}else if(_currentStackIndex == 2)
			{
				corparationView.currentState = 'tileview';
			}*/
		}
	}
}